<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流水号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['projects:SlOrder:add']"
        >创建订单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['projects:SlOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="SlOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会员" align="center" prop="customName" />
      <el-table-column label="订单流水号" align="center" prop="orderNo" />
      <el-table-column label="订单类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="订单价格" align="center" prop="orderPrice" />
      <el-table-column label="订单开启时间" align="center" prop="orderStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单结束时间" align="center" prop="orderEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['projects:SlOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['projects:SlOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改森理订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会员" prop="customName">
          <el-input v-model="form.customName" :placeholder="form.customId ? `${form.customName}(ID:${form.customId})` : '请选择用户'" readonly>
            <el-button slot="append" icon="el-icon-search" @click="openSelectCustomer"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="产品" prop="productName">
          <el-input v-model="form.productName" placeholder="请选择产品" readonly>
            <el-button slot="append" icon="el-icon-search" @click="openSelectProduct"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择订单类型">
            <el-option
              v-for="dict in dict.type.order_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="form.orderStatus" placeholder="请选择订单状态">
            <el-option
              v-for="dict in dict.type.order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单价格" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入订单价格" />
        </el-form-item>
        <el-form-item label="开启时间" prop="orderStartTime">
          <el-date-picker clearable size="small"
            v-model="form.orderStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择订单开启时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="orderEndTime">
          <el-date-picker clearable size="small"
            v-model="form.orderEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择订单结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务次数" prop="serviceTimes">
          <el-input v-model="form.serviceTimes" placeholder="请输入服务次数" />
        </el-form-item>
        <el-form-item label="剩余次数" prop="serviceLeftTimes">
          <el-input v-model="form.serviceLeftTimes" placeholder="请输入剩余次数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择会员弹框 -->
    <customer-select v-model="customerDialogVisible" @select="onCustomerSelect" />
    <!-- 选择产品弹框 -->
    <product-select v-model="productDialogVisible" @select="onProductSelect" />

  </div>
</template>

<script>
import { listSlOrder, getSlOrder, delSlOrder, addSlOrder, updateSlOrder } from "@/api/projects/SlOrder";
import CustomerSelect from "@/components/CustomerSelect";
import ProductSelect from "@/components/ProductSelect";

export default {
  name: "SlOrder",
  dicts: ['order_status', 'order_type'],
  components: {
    CustomerSelect,
    ProductSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 森理订单表格数据
      SlOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customId: null,
        productId:null,
        orderNo: null,
        orderType: null,
        orderStatus: null,
        orderPrice: null,
        orderStartTime: null,
        orderEndTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customId: [
          { required: true, message: "会员id不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "订单流水号不能为空", trigger: "blur" }
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "change" }
        ],
        orderStatus: [
          { required: true, message: "订单状态不能为空", trigger: "change" }
        ],
        orderPrice: [
          { required: true, message: "订单价格不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      },
      // 选择弹框显示状态
      customerDialogVisible: false,
      productDialogVisible: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询森理订单列表 */
    getList() {
      this.loading = true;
      listSlOrder(this.queryParams).then(response => {
        this.SlOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customId: null,
        productId:null,
        orderNo: null,
        orderType: null,
        orderStatus: null,
        orderPrice: null,
        orderStartTime: null,
        orderEndTime: null,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "创建森理订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSlOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改森理订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSlOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSlOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除森理订单编号为"' + ids + '"的数据项？').then(function() {
        return delSlOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('projects/SlOrder/export', {
        ...this.queryParams
      }, `SlOrder_${new Date().getTime()}.xlsx`)
    },
        /** 打开选择会员弹框 */
     openSelectCustomer() {
      this.customerDialogVisible = true;
    },
    
    /** 打开选择产品弹框 */
    openSelectProduct() {
      this.productDialogVisible = true;
    },

    /** 会员选择回调 */
    onCustomerSelect(customer) {
      this.form.customId = customer.id;
      this.form.customName = customer.nickname;
    },
    
    /** 产品选择回调 */
    onProductSelect(product) {
      console.log(product.id)
      this.form.productId = product.id;
      this.form.productName = product.productName;
      console.log(this.form.productId)
    }
  }
};
</script>
